<?php

namespace app\common\model;
use \think\Model;
use think\Db;

use app\api\model\Member;
use app\api\model\MemberInfo;
use app\api\model\MemberProfile;
use app\api\model\MemberLevel;

use phpqrcode\QRcode;


class Currency extends Model
{
    /**
    * 生成二维码
    */
    public function _qrcode($unique,$invitation_code){
        $unique = base64_encode($unique);
        $invitation_code = base64_encode($invitation_code);
        $url = url('api/login/ureg',"",'',true);
        
        $url = $url.'?unique='.$unique."&code=".$invitation_code;
        //var_dump(isHttps());
        //dump($url);die;
        //Loader::import('phpqrcode.phpqrcode', EXTEND_PATH);

        $dir = "upload/qrcode/";
        if(!file_exists($dir)){
            mkdir($dir, 0755, true);
        }
        $name = date("YmdHis").mt_rand(100000,999999);
        $filename = $dir.$name.'.png';  //  生成的文件名
        
        $errorCorrectionLevel = 'L';  // 纠错级别：L、M、Q、H  
        $matrixPointSize = 4; // 点的大小：1到10  
        QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
        //var_dump($ret);die;
        $img_path = '/'.$filename;
        
        return $img_path;
    }
    
    /**
    * 关闭任务的原因
    */
    public function _closeInfo(){
        $info = [
            ['id'=>1,'info'=>'关闭原因1'],
            ['id'=>2,'info'=>'关闭原因2'],
            ['id'=>3,'info'=>'关闭原因3'],
            ['id'=>4,'info'=>'关闭原因4'],
            ['id'=>5,'info'=>'关闭原因5'],
            ['id'=>6,'info'=>'关闭原因6'],
        ];
        //var_dump($info);die;
        return $info;
    }
    
    /**
    * 会员升级
    * @param mid int 会员id
    * @return bool
    */
    public function _memberUpgrade($mid){
        //$sql = "select id from sy_member_level where balance >= (select total_balance from sy_member_info  where mid = $mid) limit 1 ";
       
        $memberModel = new Member;
        $info = $memberModel->_getInfoById($mid);
        $level_id = $info['level_id'];
        
        $levelModel = new MemberLevel;
        $levelInfo = $levelModel->_getById($level_id);
        if(!$levelInfo){
            $this->error = $levelModel->getError();
            return false;
        }
        //var_dump($info['total_balance']);
        //var_dump($levelInfo['balance']);die;
        if($info['total_balance'] >= $levelInfo['balance']){
            $sql = "update sy_member set level_id = (select id from sy_member_level where balance <= (select total_balance from sy_member_info  where mid = $mid ) order by balance desc limit 1) where id = $mid and status = 1";
            
            $ret = Db::execute($sql);
            //var_dump($ret);die;
            if($ret === false){
                $this->error = '会员升级失败';
                return false;
            }
        }
        
        return true;
    }
}
